﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace EnvironmentEditor.DataModel
{
    public class WorldMap
    {
        public string Name { get; set; }

        public short Width { get; set; }

        public short Height { get; set; } 

        private MapCell[,] map;

        public WorldMap(short width, short height, string name)
        {
            this.Width = width;
            this.Height = height;
            this.Name = name;
            this.map = new MapCell[width, height];
            for (short y = 0; y < height; y++)
            {
                for (short x = 0; x < width; x++)
                {
                    this.map[x, y] = new MapCell();
                }
            }
        }

        public WorldMap(string name)
        {
            this.Name = name;
        }

        public MapCell GetItem(int x, int y)
        {
            if (x < this.Width && y < this.Height)
            {
                return this.map[x, y];
            }

            return null;
        }

        public MapCell GetOrCreateItem(int x, int y)
        {
            MapCell cell = this.GetItem(x, y);
            if (cell == null)
            {
                cell = new MapCell();
                this.AddCell(x, y, cell);
            }

            return cell;
        }

        public void AddCell(int x, int y, MapCell cell)
        { 

        }

        public void Save(TextWriter writer)
        {
            writer.Write(string.Format("{{{0},{1}", this.Width, this.Height));

            for (int y = 0; y < this.Height; y++)
            for (int x = 0; x < this.Width; x++)
            {
                writer.Write(",");

                var cell = this.GetItem(x, y);
                if (cell != null && !cell.IsEmpty)
                {
                    writer.Write(string.Format("{{{0}{1}{2}{3}{4}{5}}}", cell.North, cell.East, cell.South, cell.West, cell.Floor, cell.Sky));
                }
            }

            writer.Write("}");
        }

        public void Load(TextReader reader)
        {
            string data = reader.ReadLine();
            int commaIndex = data.IndexOf(',');
            if (commaIndex > 0)
            {
                int width = int.Parse(data.Substring(1, commaIndex - 1));

                int prevIndex = commaIndex + 1;
                commaIndex = data.IndexOf(',', prevIndex);
                int height = int.Parse(data.Substring(prevIndex, commaIndex - prevIndex));

                prevIndex = commaIndex + 1;

                this.Width = (short)width;
                this.Height = (short)height;

                this.map = new MapCell[width, height];
                for (short y = 0; y < height; y++)
                {
                    for (short x = 0; x < width; x++)
                    {
                        this.map[x, y] = new MapCell();
                    }
                }

                for (int y = 0; y < this.Height; y++)
                for (int x = 0; x < this.Width; x++)
                {
                    commaIndex = data.IndexOf(',', prevIndex);

                    if (commaIndex < 0)
                    {
                        commaIndex = data.Length - 1;
                    }

                    if (commaIndex < prevIndex)
                    {
                        break;
                    }

                    string cellData = data.Substring(prevIndex, commaIndex - prevIndex);
                    prevIndex = commaIndex + 1;

                    if (!string.IsNullOrWhiteSpace(cellData) && cellData[0] == '{' && cellData[cellData.Length - 1] == '}')
                    {
                        MapCell cell = this.map[x, y];
                        cell.North = short.Parse(cellData[1].ToString());
                        cell.East = short.Parse(cellData[2].ToString());
                        cell.South = short.Parse(cellData[3].ToString());
                        cell.West = short.Parse(cellData[4].ToString());
                        cell.Floor = short.Parse(cellData[5].ToString());
                        cell.Sky = short.Parse(cellData[6].ToString());
                    }
                }
            }
        }
    }
}
